package com.haiyangwang1.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

public class ManagerFrame extends JFrame{
    private JTextArea area;
    private JTextField txtInput;
    private JButton btn;
    private JPanel p1;
    private int index;
    Employee[] e1 = new Employee[10];
    JFrame managerlFrame = new JFrame();

    public ManagerFrame(){
        initData();

        managerlFrame.setBounds(400,200,400,300);
        Scanner input = new Scanner(System.in);
        //上面的输入框
        area = new JTextArea();
        area.setText("欢迎进入员工管理系统：\n\n 1.查看所有员工\n2.添加员工\n3.删除员工\n4.修改密码\n5.安全退出");
        //下面的输入框和按钮放在一个容器里面
        p1 = new JPanel();
        txtInput = new JTextField(20);
        btn = new JButton("确定");


        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = txtInput.getText();
                switch (str){
                    case "1":
                        list1();
                        break;
                    case "2":
                        add();
                        break;
                    case "3":
                        del();
                        break;
                    case "4":
                        reset();
                        break;
                    case "5":
                        returnMain();
                        break;
                }

            }


        });

        p1.add(txtInput);
        p1.add(btn);

        //把容器和上面的输入框加到窗口里面
        managerlFrame.add(area);
        managerlFrame.add(p1, BorderLayout.SOUTH);

        managerlFrame.setVisible(true);
        managerlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



    public void returnMain(){

        managerlFrame.dispose();

    }
    private void reset(){
        String oldpwd;
        oldpwd = JOptionPane.showInputDialog(null,"请输入你的旧密码：");
        for (int i = 0;i < e1.length;i++){
            if (e1[i].getPassword().equals(oldpwd)){
                String newpwd = JOptionPane.showInputDialog(null,"请输入你的新密码：");
                String newpwd1 = JOptionPane.showInputDialog(null,"请再次输入你的新密码：");
                if (newpwd.equals(newpwd1)){
                    e1[i].setPassword(newpwd);
                }else {
                    JOptionPane.showMessageDialog(null,"两次密码不一致");
                }
            }else {
                JOptionPane.showMessageDialog(null,"旧密码错误");

            }
            break;
        }
    }
    private void del(){
        String name = JOptionPane.showInputDialog(null,"请输入需要删除的员工姓名：");
        int index = -1;
        for (int i = 0;i < index;i++){
            if (e1[i].getName().equals(name)){
                index++;
            }
            if (index!=-1){
                e1[i]=new Employee();
                index--;
            }else {
                JOptionPane.showMessageDialog(null,"查无此人");

            }
            break;
        }
    }
    private void add(){
        String name = JOptionPane.showInputDialog(null,"请输入你的姓名：");
        String pwd = JOptionPane.showInputDialog(null,"请输入你的密码：");
        String position = JOptionPane.showInputDialog(null,"请输入你的职位：");
//        String str2 = name+pwd+position;
        Employee employee = new Employee(name,pwd,position);

        for (int i =0;i<index;i++){
            if (e1[i]==null){
                e1[i]=employee;
                index++;
                break;
            }
        }

    }
    private void list1() {
        //查看所有员工
        String str1 = "姓名      密码      职位\n";
        for (int i = 0;i<e1.length;i++){
            if (e1[i]!=null){
                Employee employees = e1[i];
                str1+=employees.getName()+"     "+employees.getPassword()+"        "+employees.getPosition()+"\n";

            }
        }
        JOptionPane.showMessageDialog(null,str1);
    }
    public void initData(){

        e1[0] = new Employee("jack","666","管理员");
        index++;
        e1[1] = new Employee("lily","555","员工");
        index++;
    }
}
